package com.jinghangzz.ssm.demo.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jinghangzz.ssm.common.util.PageInfoUtil;
import com.jinghangzz.ssm.demo.dao.IADemoDynastyDao;
import com.jinghangzz.ssm.demo.dao.IADemoKingDao;
import com.jinghangzz.ssm.demo.pojo.ADemoDynasty;
import com.jinghangzz.ssm.demo.pojo.ADemoKing;
import com.jinghangzz.ssm.demo.service.IDemoDbService;

/**
 * Service的实现类
 * @author TeaBig
 */
@Service("demoDbService")
public class DemoDbServiceImpl implements IDemoDbService
{
	@Resource
	private IADemoDynastyDao demoDynastyDao;
	@Resource
	private IADemoKingDao demoKingDao;

	@Override
	public int saveOneDynastyService(ADemoDynasty demoDynasty)
	{
		int res =  this.demoDynastyDao.saveOneDao(demoDynasty);
		return res ; 
	}

	@Override
	public int updateOneDynastyService(ADemoDynasty demoDynasty)
	{
		return this.demoDynastyDao.updateOneDao(demoDynasty);
	}

	@Override
	public int deleteOneDynastyService(ADemoDynasty demoDynasty)
	{
		return this.demoDynastyDao.deleteOneDao(demoDynasty);
	}

	@Override
	public ADemoDynasty findOneDynastyService(Map<String, Object> condMap)
	{
		return this.demoDynastyDao.findOneDao(condMap);
	}

	@Override
	public List<ADemoDynasty> findCondListDynastyService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 添加% */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%"+ condMap.get("keyword") +"%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 把这句话加到调用dao方法之前 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ADemoDynasty> list = this.demoDynastyDao.findCondListDao(condMap);
			/* 获取到总条数,把总条数放到我的PageinfoUtil中 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return list ; 
		}
		/* pageInfoUtil如果不为空,就分页
		 * 如果为空,就不分页 */
		return this.demoDynastyDao.findCondListDao(condMap);
	}
	
	@Override
	public int saveOneKingService(ADemoKing demoKing)
	{
		int res =  this.demoKingDao.saveOneDao(demoKing);
		return res ; 
	}

	@Override
	public int updateOneKingService(ADemoKing demoKing)
	{
		return this.demoKingDao.updateOneDao(demoKing);
	}

	@Override
	public int deleteOneKingService(ADemoKing demoKing)
	{
		return this.demoKingDao.deleteOneDao(demoKing);
	}

	@Override
	public ADemoKing findOneKingService(Map<String, Object> condMap)
	{
		return this.demoKingDao.findOneDao(condMap);
	}

	@Override
	public List<ADemoKing> findCondListKingService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 添加% */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%"+ condMap.get("keyword") +"%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 把这句话加到调用dao方法之前 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ADemoKing> list = this.demoKingDao.findCondListDao(condMap);
			/* 获取到总条数,把总条数放到我的PageinfoUtil中 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return list ; 
		}
		/* pageInfoUtil如果不为空,就分页
		 * 如果为空,就不分页 */
		return this.demoKingDao.findCondListDao(condMap);
	}

}
